package com.project.server.companies;

import com.project.common.annotation.Log;
import com.project.common.core.controller.BaseController;
import com.project.common.core.domain.AjaxResult;
import com.project.common.core.page.TableDataInfo;
import com.project.common.enums.BusinessType;
import com.project.common.utils.StringUtils;
import com.project.common.utils.poi.ExcelUtil;
import com.project.server.domain.companies.TCompanyStaff;
import com.project.server.service.companies.ITCompanyService;
import com.project.server.service.companies.ITCompanyStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工信息Controller
 *
 * @author project
 * @date 2021-07-02
 */
@RestController
@RequestMapping("/server/staff")
public class TCompanyStaffController extends BaseController {

    @Autowired
    private ITCompanyStaffService tCompanyStaffService;

    @Autowired
    private ITCompanyService tCompanyService;

    /**
     * 查询员工信息列表
     */
    @PreAuthorize("@ss.hasPermi('server:staff:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCompanyStaff tCompanyStaff) {
        startPage();
        List<TCompanyStaff> list = tCompanyStaffService.selectTCompanyStaffList(tCompanyStaff);
        return getDataTable(list);
    }


    /**
     * 导出员工信息列表
     */
    @PreAuthorize("@ss.hasPermi('server:staff:export')")
    @Log(title = "员工信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TCompanyStaff tCompanyStaff) {
        List<TCompanyStaff> list = tCompanyStaffService.selectTCompanyStaffList(tCompanyStaff);
        ExcelUtil<TCompanyStaff> util = new ExcelUtil<TCompanyStaff>(TCompanyStaff.class);
        return util.exportExcel(list, "企业员工列表");
    }

    /**
     * 获取员工信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('server:staff:query')")
    @GetMapping(value = {"/", "/{staffId}"})
    public AjaxResult getInfo(@PathVariable(value = "staffId", required = false) Long staffId) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("comps", tCompanyService.selectTCompanyAll());
        if (StringUtils.isNotNull(staffId)) {
            ajax.put(AjaxResult.DATA_TAG, tCompanyStaffService.selectTCompanyStaffById(staffId));
            ajax.put("compId", tCompanyService.selectTCompanyListByStaffId(staffId));
        }
        return ajax;
    }

    /**
     * 新增员工信息
     */
    @PreAuthorize("@ss.hasPermi('server:staff:add')")
    @Log(title = "员工信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TCompanyStaff tCompanyStaff) {
        return toAjax(tCompanyStaffService.insertTCompanyStaff(tCompanyStaff));
    }

    /**
     * 修改员工信息
     */
    @PreAuthorize("@ss.hasPermi('server:staff:edit')")
    @Log(title = "员工信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TCompanyStaff tCompanyStaff) {
        return toAjax(tCompanyStaffService.updateTCompanyStaff(tCompanyStaff));
    }

    /**
     * 删除员工信息
     */
    @PreAuthorize("@ss.hasPermi('server:staff:remove')")
    @Log(title = "员工信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{staffIds}")
    public AjaxResult remove(@PathVariable Long[] staffIds) {
        return toAjax(tCompanyStaffService.deleteTCompanyStaffByIds(staffIds));
    }
}
